package com.hksj.Intelligentmanagement.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hksj.Intelligentmanagement.common.Result;
import com.hksj.Intelligentmanagement.dto.AlarmConfigurationDTO;
import com.hksj.Intelligentmanagement.entity.AlarmConfigurationEntity;
import com.hksj.Intelligentmanagement.mapper.AlarmConfigurationMapper;
import com.hksj.Intelligentmanagement.vo.AlarmConfigurationVo;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName AlarmConfigurationService
 * @Author zengke
 * @Date 2023/7/17 16:14
 */
@Service
public class AlarmConfigurationService extends ServiceImpl<AlarmConfigurationMapper, AlarmConfigurationEntity> {
    //将DozerBeanMapper注入进来
    @Autowired
    private DozerBeanMapper mapper;

    //将AlarmConfigurationMapper注入
    @Autowired
    private AlarmConfigurationMapper alarmConfigurationMapper;


    //新增告警配置
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result createAlarmConfiguration(AlarmConfigurationDTO alarmConfigurationDTO){

        //构造数据库中字段等于告警值代码的查询条件
        LambdaQueryWrapper<AlarmConfigurationEntity> wrapper2 = new LambdaQueryWrapper<AlarmConfigurationEntity>()
                .eq(AlarmConfigurationEntity::getAlarmValueCode, alarmConfigurationDTO.getAlarmValueCode())
                .eq(AlarmConfigurationEntity::getDepartmentId,alarmConfigurationDTO.getDepartmentId())
                .eq(AlarmConfigurationEntity::getBuildingId,alarmConfigurationDTO.getBuildingId());

        //通过告警值名称以及告警值代码查询该记录在数据库中的数量,若其中有一个查询结果超过了1则数据库中存在重复的告警配置
        if (baseMapper.selectCount(wrapper2) > 0){
            return Result.error("存在相同的告警配置");
        }

        //新增报警配置
        //1.通过DozerBeanMapper先将AlarmConfigurationDTO转为AlarmConfigurationEntity
        AlarmConfigurationEntity alarmConfigurationEntity = mapper.map(alarmConfigurationDTO, AlarmConfigurationEntity.class);

        //2.调用save方法新增数据
        if (this.save(alarmConfigurationEntity)){
            return Result.ok("新增告警配置成功");
        }
        return Result.error("新增失败");
    }

    //修改告警配置
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result updateAlarmConfiguration(AlarmConfigurationDTO alarmConfigurationDTO){
        //更新告警配置
        //1.通过DozerBeanMapper先将AlarmConfigurationDTO转为AlarmConfigurationEntity
        AlarmConfigurationEntity alarmConfigurationEntity = mapper.map(alarmConfigurationDTO, AlarmConfigurationEntity.class);

        //2.调用updateById方法更新数据库
        if (this.updateById(alarmConfigurationEntity)){
            return Result.ok("更新告警配置成功");
        }
        return Result.error("更新失败");
    }

    //删除告警配置
    public Result deleteAlarmConfiguration(String id){
        //调用removeById方法删除
        if (this.removeById(id)){
            return Result.ok("删除告警配置成功");
        }
        return Result.error("删除失败");
    }

    //根据气体id查询告警配置信息
    public List<AlarmConfigurationVo> queryAlarmConfigurationList(AlarmConfigurationDTO alarmConfigurationDTO){
        //调用alarmConfigurationMapper的queryAlarmConfigurationList方法动态查询告警配置信息列表
        List<AlarmConfigurationVo> alarmConfigurationVos = alarmConfigurationMapper.queryAlarmConfigurationList(alarmConfigurationDTO);

        //返回查询结果
        return alarmConfigurationVos;

    }



}
